/*! \file DeviceContext.cpp
 *	\brief 
 *
 *	created:	2010/07/12
 *	created:	12:7:2010   18:57
 *	filename: 	C:\src\tactics-engine\libs\gd_dx11\DeviceContext.cpp
 *	file path:	C:\src\tactics-engine\libs\gd_dx11
 *	file base:	DeviceContext
 *	file ext:	cpp
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */
#include "gd_dx11/stdafx.h"
#include "gd_dx11/DeviceContext.h"

#include <app_win/Window.h>

namespace tt{
namespace gd_dx11{

DeviceContext::DeviceContext( const utils::COMPointer<ID3D11DeviceContext>& context )
: m_context(context)
, m_renderTarget()
{
	m_context->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
}

DeviceContext::~DeviceContext()
{
	m_context->ClearState();
	m_context->Flush();

	m_renderTarget.reset();
	m_context.reset();
}

void DeviceContext::setRenderTarget( const utils::COMPointer<ID3D11RenderTargetView>& renderTarget )
{
	m_renderTarget = renderTarget;

	// Bind the view
	UInt renderTargetViewsCount = 1;
	ID3D11RenderTargetView* renderTargetViews[] = {m_renderTarget.get()};
	m_context->OMSetRenderTargets( renderTargetViewsCount, renderTargetViews, NULL );
}

void DeviceContext::setViewport(const SharedPtr<app_win::Window>& window)
{
	const app::WindowBounds& windowBounds = window->bounds();
	const app::WindowSize& windowSize = windowBounds.size();

	// Setup the viewport
	D3D11_VIEWPORT vp;
	vp.Width = (FLOAT)windowSize.x;
	vp.Height = (FLOAT)windowSize.y;
	vp.MinDepth = 0.0f;
	vp.MaxDepth = 1.0f;
	vp.TopLeftX = (FLOAT)windowBounds.min.x;
	vp.TopLeftY = (FLOAT)windowBounds.min.y;
	m_context->RSSetViewports( 1, &vp );
}

void DeviceContext::setVertexShader( utils::COMPointer<ID3D11VertexShader>& vertexShader )
{
	m_context->VSSetShader(vertexShader.get(), NULL, 0);
}

void DeviceContext::setInputLayout( utils::COMPointer<ID3D11InputLayout>& inputLayout )
{
	m_context->IASetInputLayout(inputLayout.get());
}

void DeviceContext::setPixelShader( utils::COMPointer<ID3D11PixelShader>& pixelShader )
{
	m_context->PSSetShader(pixelShader.get(), NULL, 0);
}

//////////////////////////////////////////////////////////////////////////
void DeviceContext::onClear()
{
	// Clear the render target to dark blue
	float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red, green, blue, alpha
	m_context->ClearRenderTargetView( m_renderTarget.get(), ClearColor );
}

} //namespace gd_dx11
} //namespace tt